package com.shangxian.giveithome.controller;


import com.shangxian.giveithome.annoaction.CheckLoginStatus;
import com.shangxian.giveithome.code.PetCode;
import com.shangxian.giveithome.entity.AsylumCreate;
import com.shangxian.giveithome.entity.AsylumStatus;
import com.shangxian.giveithome.entity.AsylumUpdate;
import com.shangxian.giveithome.exception.ExceptionUtils;
import com.shangxian.giveithome.result.Result;
import com.shangxian.giveithome.service.AsylumService;
import com.shangxian.giveithome.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@Api(tags = "收容APi")
@RestController
@RequestMapping("/asylum")
public class AsylumController {

    @Autowired
    private AsylumService asylumService;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private HttpServletRequest httpServletRequest;

    @ApiOperation(value = "查看收容详情")
    @GetMapping(value = "/queryByAsylumDetails/{id}")
    @CheckLoginStatus
    public Result queryAsylumDetails(@PathVariable Long id) {
        return Result.success(asylumService.queryByAsylumDetails(id));
    }

    @ApiOperation(value = "创建收容记录")
    @PostMapping(value = "/createAsylum")
    public Result createAsylum(@RequestBody @Validated AsylumCreate asylumCreate) {
        Long aLong = redisUtils.getMechanismCheck(httpServletRequest);
        if (aLong == -1L) {
            ExceptionUtils.error(PetCode.NOT_AUTH_UP_DATA);
        }
        asylumCreate.setMeId(aLong);
        asylumService.create(asylumCreate);
        return Result.success();
    }

    @ApiOperation(value = "修改收容记录")
    @PutMapping(value = "/updateAsylum")
    public Result updateAsylum(@RequestBody @Validated AsylumUpdate asylumUpdate) {
        Long aLong = redisUtils.getMechanismCheck(httpServletRequest);
        if (aLong == -1L) {
            ExceptionUtils.error(PetCode.NOT_AUTH_UP_DATA);
        }
        asylumUpdate.setMeId(aLong);
        asylumService.update(asylumUpdate);
        return Result.success();
    }

    @ApiOperation(value = "删除收容记录")
    @DeleteMapping(value = "/deleteAsylum/{id}")
    public void deleteAsylum(@Valid @PathVariable @NotNull(message = "收容id不能为空") Long id) {
        Long aLong = redisUtils.getMechanismCheck(httpServletRequest);
        if (aLong == -1L) {
            ExceptionUtils.error(PetCode.NOT_AUTH_DEL_DATA);
        }
        asylumService.delete(id, aLong);
    }

    @ApiOperation(value = "修改收容状态")
    @PutMapping("/updateAsylumStatus")
    public  Result updateAsylumStatus(@RequestBody AsylumStatus asylumStatus){
        Long aLong = redisUtils.getMechanismCheck(httpServletRequest);
        if (aLong == -1L) {
            ExceptionUtils.error(PetCode.NOT_AUTH_UP_DATA);
        }
        asylumStatus.setALong(aLong);
        asylumService.updateStatus(asylumStatus);
        return Result.success();
    }
}
